//
//  VideoADView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/9.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

protocol VideoADViewDelegate: NSObjectProtocol {
    func didSelectIndex(index: NSInteger, videoADView: VideoADView)
}

class VideoADView: UIView, UITableViewDelegate, UITableViewDataSource, AddCornerUIRectCorner {
    
    private let allWidth: CGFloat = ScreenWidth * 0.5
    private let topViewHeight: CGFloat = 27
    private let centerViewHeight: CGFloat = 84
    private let leftMaring: CGFloat = 14
    private let lineHeight: CGFloat = 1
    private let bottomButtonHeight: CGFloat = 25
    private let topView = UIView()
    weak var delegate: VideoADViewDelegate?
     let commodityTableView = UITableView(frame: .zero, style: .plain)
    let bottomButton = UIButton(type: .custom)
    
    /// 展示数据列表
    ///
    /// - Parameters:
    ///   - data: 临时用的数据
    func showData(data: [String]) {
        UIApplication.shared.keyWindow?.addSubview(self)
        self.backgroundColor = UIColor.clear
        let height = topViewHeight + centerViewHeight * 2 + bottomButtonHeight
        self.frame = CGRect(x: ScreenHeight, y: ScreenWidth / 2 - height / 2, width: allWidth, height: height)
        UIView.animate(withDuration: TimeInterval(anTIME)) {
            self.frame.origin.x = ScreenHeight - self.allWidth
        }
        
        addCornerWithView(aView: self, aCorners: [.topLeft, .bottomLeft], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
        
        setTopView()
        setTableView()
        setBottomView()
    }
    
   private func setTopView() {
        
        topView.backgroundColor = UIColor(hex: 0x000000, alpha: 0.5)
        self.addSubview(topView)
        topView.frame = CGRect(x: 0, y: 0, width: allWidth, height: topViewHeight)
        let titleLabel = UILabel()
        titleLabel.text = "推荐购买"
        titleLabel.font = UIFont.systemFont(ofSize: 16)
        titleLabel.textColor = UIColor.black
        titleLabel.sizeToFit()
        titleLabel.frame.origin = CGPoint(x: leftMaring, y: topViewHeight / 2 - titleLabel.height / 2)
        topView.addSubview(titleLabel)
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor(hex: 0x000000, alpha: 0.3)
        lineView.frame = CGRect(x: 0, y: topView.height - lineHeight, width: topView.width, height: lineHeight)
        topView.addSubview(lineView)
    }
    
    private func setTableView() {
        commodityTableView.frame = CGRect(x: 0, y: topView.bottom, width: allWidth, height: centerViewHeight * 2)
        commodityTableView.backgroundColor = UIColor.clear
        commodityTableView.dataSource = self
        commodityTableView.delegate = self
        commodityTableView.showsVerticalScrollIndicator = false
        commodityTableView.separatorStyle = .none
        commodityTableView.register(UINib(nibName: "\(VideoADTableViewCell.self)", bundle: nil), forCellReuseIdentifier: "cell")
        self.addSubview(commodityTableView)
    }
    
    private func setBottomView() {
        
        bottomButton.backgroundColor = UIColor(hex: 0x000000, alpha: 0.4)
        bottomButton.setImage(UIImage(named: "common_prohibit"), for: .normal)
        bottomButton.frame = CGRect(x: 0, y: commodityTableView.bottom, width: allWidth, height: bottomButtonHeight)
        bottomButton.addTarget(self, action: #selector(cancelTap), for: .touchUpInside)
        self.addSubview(bottomButton)
    }
    
    // MARK: - tableViewDataSource
    internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? VideoADTableViewCell
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return centerViewHeight
    }
    
    internal func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.delegate?.didSelectIndex(index: indexPath.row, videoADView: self)
    }
    
    // MARK: - otherFunction
    @objc func cancelTap() {
        hide()
    }
    
    open func hide() {
        UIView.animate(withDuration: TimeInterval(anTIME), animations: {
            self.frame.origin.x = ScreenHeight
        }) { (make) in
            self.removeFromSuperview()
        }
    }
}
